<template>
  <div class="container">
    <div class="app-container1" >
      <div class="parent-container">
        <h4>下部线</h4>
      </div>
      <el-button
        type="primary"
        icon="el-icon-plus"
        style="margin-bottom: 5px;"
        @click="handleAdd"
      >添加</el-button>

      <el-table
        v-loading="loading"
        :data="filteredTableList1"
        @selection-change="handleSelectionChange"
        border
        style="width: 100%;"
        :show-overflow-tooltip="true"
        empty-text="暂无数据"
      >
        <el-table-column label="序" align="center" prop="sort" />
        <el-table-column label="名称" align="center" prop="name"/>
        <el-table-column label="状态" align="center" prop="status"/>
        <el-table-column label="提出者" align="center" prop="createBy" />
        <el-table-column label="价格" align="center" prop="price"/>
        <el-table-column
          label="操作"
          align="center"
          width="160"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:user:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:user:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="序" prop="sort">
                <el-select v-model="form.sort" placeholder="请选择第几序" >
                  <el-option
                    v-for="item in options1"
                    :key="item.value"
                    :label="item.l"
                    :value="item.value"
                    @change="handleSelectChange"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="名称" prop="name">
                <el-select v-model="form.name" placeholder="请选择名称">
                  <el-option
                    v-for="item in dropdownData"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
      <el-table>
        <el-table-column label="总价" align="center" prop="sort" />
      </el-table>
    </div>



    <div class="app-container2" >
      <div class="parent-container">
        <h4>主线</h4>
      </div>
      <el-button
        type="primary"
        icon="el-icon-plus"
        style="margin-bottom: 5px;"
        @click="handleAdd"
      >添加</el-button>
      <el-table
        v-loading="loading"
        :data="filteredTableList2"
        @selection-change="handleSelectionChange"
        border
        style="width: 100%;"
        :show-overflow-tooltip="true"
        empty-text="暂无数据"
      >
        <el-table-column label="序" align="center" prop="sort" />
        <el-table-column label="名称" align="center" prop="name"/>
        <el-table-column label="状态" align="center" prop="status"/>
        <el-table-column label="提出者" align="center" prop="createBy" />
        <el-table-column label="价格" align="center" prop="price"/>
        <el-table-column
          label="操作"
          align="center"
          width="160"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:user:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:user:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-table>
        <el-table-column label="总价" align="center" prop="sort" />
      </el-table>
    </div>




    <div class="app-container1" >
      <div class="parent-container">
        <h4>侧围二级</h4>
      </div>
      <el-button
        type="primary"
        icon="el-icon-plus"
        style="margin-bottom: 5px;"
        @click="handleAdd"
      >添加</el-button>
      <el-table
        v-loading="loading"
        :data="filteredTableList3"
        @selection-change="handleSelectionChange"
        border
        style="width: 100%;"
        :show-overflow-tooltip="true"
        empty-text="暂无数据"
      >
        <el-table-column label="序" align="center" prop="sort" />
        <el-table-column label="名称" align="center" prop="name"/>
        <el-table-column label="状态" align="center" prop="status"/>
        <el-table-column label="提出者" align="center" prop="createBy" />
        <el-table-column label="价格" align="center" prop="price"/>
        <el-table-column
          label="操作"
          align="center"
          width="160"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:user:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:user:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-table>
        <el-table-column label="总价" align="center" prop="sort" />
      </el-table>
    </div>



    <div class="app-container2" >
      <div class="parent-container">
        <h4>发舱二级</h4>
      </div>
      <el-button
        type="primary"
        icon="el-icon-plus"
        style="margin-bottom: 5px;"
        @click="handleAdd"
      >添加</el-button>
      <el-table
        v-loading="loading"
        :data="filteredTableList4"
        @selection-change="handleSelectionChange"
        border
        style="width: 100%;"
        :show-overflow-tooltip="true"
        empty-text="暂无数据"
      >
        <el-table-column label="序" align="center" prop="sort" />
        <el-table-column label="名称" align="center" prop="name"/>
        <el-table-column label="状态" align="center" prop="status"/>
        <el-table-column label="提出者" align="center" prop="createBy" />
        <el-table-column label="价格" align="center" prop="price"/>
        <el-table-column
          label="操作"
          align="center"
          width="160"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:user:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:user:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
     <el-table>
       <el-table-column label="总价" align="center" prop="sort" />
     </el-table>

    </div>
  </div>
</template>

<script>
import {listPark22,getid,insertpark22,updatepark22,deletepark22} from "@/api/park2/park22";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { getToken } from "@/utils/auth";
import axios from "axios";

export default {
  name:"Park22",
  dicts: ['sys_normal_disable'],
  data(){
    return{
      // 遮罩层
      loading: true,
      // 唯一标识符
      uniqueId: "",
      // 选中数组
      ids: [],
      // 菜单列表
      menuOptions: [],
      // 存储用户选择的数据项
      multipleSelection: [],
      // 选中表数组
      tableNames: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch:true,
      // 总条数
      total: 0,
      // 表数据
      tableList: [],
      // 日期范围
      dateRange: "",
      // 部门树选项
      deptOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 弹出层标题
      title: "",
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },

      // 用户导入参数
      upload:{
        // // 是否显示弹出层（用户导入）
        open: false,
        // // 弹出层标题（用户导入）
        title: "",
        // // 是否禁用上传
        isUploading: false,
        // // 是否更新已经存在的用户数据
        updateSupport: 0,
        // // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/park22/park22/importData"
      },

      // 查询参数
      queryParams:{
        pageNum: 1,
        pageSize: 10,
        ipaddr: undefined,
        name:undefined,
        tableName: undefined,
        tableComment: '' // 假设这是下拉列表绑定的模型
      },
      options1: [
        // 下拉列表的选项数组
        { value: '1', label: '1' },
        { value: '2', label: '2' },
        { value: '3', label: '3' },
        { value: '4', label: '4' },
        { value: '5', label: '5' },
        { value: '6', label: '6' },
        { value: '7', label: '7' },
        // 可以继续添加更多选项
      ],
      dropdownData: [],
    }
  },
  created() {
    this.getList();
    this.getDropdownData();
  },
  activated() {
    const time = this.$route.query.t;
    if (time != null && time != this.uniqueId) {
      this.uniqueId = time;
      this.queryParams.pageNum = Number(this.$route.query.pageNum);
      this.getList();
    }
  },

  computed:{
    filteredTableList1() {
      return this.tableList.filter(item => item.region === '下部线');
    },
    filteredTableList2() {
      return this.tableList.filter(item => item.region === '主线');
    },
    filteredTableList3() {
      return this.tableList.filter(item => item.region === '侧围二级');
    },
    filteredTableList4() {
      return this.tableList.filter(item => item.region === '发舱二级');
    },

  },

  mounted(){
    this.getDropdownData();
  },

  methods:{
    getList() {
      this.loading = true;
      listPark22(this.queryParams).then(response => {
        this.tableList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        name: undefined,
        status: undefined,
        region: undefined,
        state: undefined,
        notes: undefined,
        sort: undefined,
      };
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
      this.multipleSelection =selection;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getid(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改数据";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updatepark22(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            insertpark22(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除ID为"' + ids + '"的数据项？').then(function() {
        return deletepark22(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    /** 导出按钮操作 */
    handleExport() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          message:'请选择要导出的数据',
          type:'warning',
        });
        return ;
      }
      const ids=this.multipleSelection.map(item => item.id).join(',');
      this.download('park22/park22/export', {ids}, `user_${new Date().getTime()}.xlsx`)
    },

    handleSelectChange(newValue) {
      // 当选择的值变化时，这里将接收新的值作为参数
      console.log('选中的值变化为:', newValue);
      // 在这里可以执行进一步的逻辑处理
    },

    /**获取后端数据作为下拉框数据**/
    async getDropdownData() {
        try {
          let res = await axios.get('/dev-api/park22/park22/dropdown-data', {
            headers: {
              'Authorization': `Bearer ${getToken()}`
            }
          });
          // 确保 res.data.data 是一个对象
          const result = res.data.data;
          // 检查 result 是否存在且为数组
          if (result && Array.isArray(result)) {
            this.dropdownData = result.map(element => {
              return { label: element.name, value: element.name };
            });
          } else {
            console.error('Unexpected data format:', result);
          }
        } catch (error) {
          console.error('Error fetching dropdown data:', error);
        }
      },

    }
}
</script>

<style>
.container {
  display: flex;
  justify-content: center; /* 水平居中子div */
  flex-wrap: wrap; /* 允许换行显示 */
  margin: -5px; /* 根据间隔调整外边距，以抵消子div的外边距 */
}

.app-container1, .app-container2 {
  border: 1px solid #dcdfe6; /* 边框颜色，这里使用了 Element UI 的默认边框颜色 */
  background-color: #f5f7fa; /* 背景颜色，一个浅灰色 */
  border-radius: 4px; /* 边框圆角 */
  padding: 10px; /* 内边距 */
  margin-bottom: 20px; /* 外边距，与之前保持一致 */
  box-sizing: border-box; /* 边框计算在宽度内 */
  width: calc(50% - 10px); /* 保持宽度 */
  margin: 5px; /* 保持间隔 */
}

/* 如果你有一个单独的父容器，并且希望<h4>标签在其中居中显示，可以保留以下样式 */
.parent-container {
  text-align: center;
}
</style>
